package com.nisco.dms.service;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nisco.dms.util.dwz.Page;
import com.nisco.dms.util.dwz.PageUtils;

/**
 * 
 * @author joey
 * 
 */
// Spring Service Bean的标识.
@Component
@Transactional(readOnly = true)
public class BaseService<T, ID extends Serializable> {

	private JpaRepository<T, ID> jpaRepository;

	public BaseService() {

	}

	public BaseService(JpaRepository<T, ID> jpaRepository) {
		this.jpaRepository = jpaRepository;
	}

	@Transactional
	public void save(T entity) {
		jpaRepository.save(entity);
	}

	@Transactional
	public void update(T entity) {
		jpaRepository.save(entity);
	}

	@Transactional
	public void delete(ID id) {
		jpaRepository.delete(id);
	}

	public T get(ID id) {
		return jpaRepository.findOne(id);
	}

	public List<T> findAll() {
		return jpaRepository.findAll();
	}

	public List<T> findAll(Page page) {
		org.springframework.data.domain.Page<T> springDataPage = jpaRepository.findAll(PageUtils.createPageable(page));
		PageUtils.injectPageProperties(page, springDataPage);
		return springDataPage.getContent();
	}
}
